/**************************************************************************
 * @file usr_buck_boost_ctrl.h
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-07-18
 * 
 * @copyright Copyright (c) 2025
 * 
 *************************************************************************/
#ifndef USR_BUCK_BOOST_CTRL__H_H
#define USR_BUCK_BOOST_CTRL__H_H

#ifdef __cplusplus
extern "C"{
#endif
#include<stdint.h>
#include<stdio.h>
#include "usr_adc_handle.h"
#include "usr_pid.h"

typedef uint32_t HrpwmCmpValue;

typedef enum{
    WORK_MODE_NULL=0,
    BUCK_W_MODE,
    BUCK_BOOST_W_MIX_UP_M,
    BUCK_BOOST_W_MIX_DW_M,
    BOOST_W_MODE,
    WORK_MODE_MAX__
}WorkModeT;

typedef struct usr_buck_boost_dat_tt {
    usr_IncPID pid_vo;
    usr_IncPID pid_io;
    usr_int_pi int_pi_vo;
    usr_int_pi int_pi_io;

    uint16_t ref_vo;
    uint16_t ref_io;

    uint32_t buck_period;
    uint32_t boost_period;
    WorkModeT wk_mode;
    WorkModeT next_wk_mode;
    float pwm_duty;
    int32_t pwm_cmp_value;
    // float boost_duty;

} UsrBuckBoostDatT;

#define BOOST_M_BUCK_MAX_DUBY (0.9f) /*90%*/
#define THRESHOLD             (GET_ADC_VOLUE_BY_OUT_VOL(300 /*mV*/))
#define INPUT_UVP_THRESHOLD   (GET_ADC_VOLUE_BY_OUT_VOL(4100 /*mV*/))
#define INPUT_OVP_THRESHOLD   (GET_ADC_VOLUE_BY_OUT_VOL(36000 /*mV*/))

#define ADC_MAP_PWM_MAX_DUTY ((BUCK_MAX_DUTY_CYCLE + 0.005f))//(ADC_ACCURACY * (BUCK_MAX_DUTY_CYCLE + 0.005f))
#define ADC_MAP_PWM_MIN_DUTY (-ADC_MAP_PWM_MAX_DUTY)//(ADC_ACCURACY * (BUCK_MINI_DUTY_CYCLE - (0.005f)))
#define ADC_MAP_PWM_INC_MAX  ( 0.08f)//(ADC_ACCURACY * 0.3f /*40%*/)
#define ADC_MAP_FRACTION     (0.000244140625f /*1.0f / ADC_ACCURACY*/)
#define ADC_MAP_MAX_RES      (ADC_ACCURACY)

void usr_buck_boost_work_mode_handle(UsrBuckBoostDatT *buck_boost, uint16_t in_vol_adc_value,uint16_t out_vol_adc_value,uint16_t out_curr_adc_value);

void usr_set_output_vol(UsrBuckBoostDatT *buck_boost,uint32_t target_vol);
void usr_set_output_current(UsrBuckBoostDatT *buck_boost,uint32_t target_curr/*mA*/);

void usr_buck_boost_pid_init(UsrBuckBoostDatT *buck_boost);
float get_init_pi_output_add_value(WorkModeT curr_wk_mode, WorkModeT next_wk_mode, float current_fixed_duty, float adj_duty);


void usr_buck_boost_int_pid_init(UsrBuckBoostDatT *buck_boost);
void usr_buck_boost_work_mode_handle_int(UsrBuckBoostDatT *pbuck_boost, uint16_t in_vol_adc_value, uint16_t out_vol_adc_value, uint16_t out_curr_adc_value);
int32_t get_init_int_pi_output_add_value(WorkModeT curr_wk_mode, WorkModeT next_wk_mode, int32_t current_fixed_duty, int32_t adj_duty);
#ifdef __cplusplus
}
#endif

#endif //USR_BUCK_BOOST_CTRL__H_H


